package com.daka.controller;

import com.daka.entity.User;
import com.daka.enumerate.StatusCode;
import com.daka.service.UserService;
import com.daka.service.impl.RedisClient;
import com.daka.tools.MD5Util;
import com.daka.vo.response.BaseResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;

/**
 * Created by yangx
 * Date: 2016/7/25 0025
 * Time: 11:19
 */
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource(name = "userService")
    private UserService userService;

    @Resource(name = "redisClient")
    private RedisClient redisClient;

    /**
     * 发送短信验证码
     * @param mobile
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/sendCode")
    public BaseResponseVo sendCode(@RequestParam(value = "mobile")String mobile) {

        if(StringUtils.isBlank(mobile)){
            return  new BaseResponseVo(StatusCode.PARAMETER_INVALID.getCode(), StatusCode.PARAMETER_INVALID.getMessage());
        }
        String code = userService.sendVerifierCode(mobile);
//        String code1 = (String) redisClient.get("resetPassword", mobile, String.class);
//        System.out.println("code"+code);
//        System.out.println("code1"+code1);
        if (code.length() > 0) {
            return new BaseResponseVo(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
        }
        return new BaseResponseVo(StatusCode.SEND_CODE_ERROR.getCode(), StatusCode.SEND_CODE_ERROR.getMessage());
    }

    /**
     * 注册用户
     * @param user
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/register")
    public BaseResponseVo register(User user,@RequestParam(value = "code")String code) {
        try {
            if (StringUtils.isBlank(user.getMobile())) {
                return new BaseResponseVo(StatusCode.PARAMETER_INVALID.getCode(), StatusCode.PARAMETER_INVALID.getMessage());
            }

            User user1 = userService.queryByMobile(user.getMobile());
            if (user1 != null) {
                return new BaseResponseVo(StatusCode.USER_EXITS.getCode(), StatusCode.USER_EXITS.getMessage());
            }
            //验证code
            String code1 = (String) redisClient.get("resetPassword", user.getMobile(), String.class);
            if (!code.equalsIgnoreCase(code1)) {
                return new BaseResponseVo(StatusCode.CODE_ERROR.getCode(), StatusCode.CODE_ERROR.getMessage());
            }

            int result = userService.insert(user);
            if (result == 0) {
                new BaseResponseVo(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMessage());
            }
            return new BaseResponseVo(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new BaseResponseVo(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }

    /**
     * 用户登录
     * @param mobile
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login")
    public BaseResponseVo login(@RequestParam(value = "mobile")String mobile,@RequestParam(value = "password")String password) {

        User user = userService.queryByMobile(mobile);
        if(user==null){
            return new BaseResponseVo(StatusCode.USER_NOT_EXITS.getCode(), StatusCode.USER_NOT_EXITS.getMessage());
        }
        if(user.getPassword().equalsIgnoreCase(MD5Util.MD5(password))){
            return  new BaseResponseVo(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
        }else{
            return new BaseResponseVo(StatusCode.PASSWORD_ERROR.getCode(), StatusCode.PASSWORD_ERROR.getMessage());
        }
    }

    /**
     * 修改密码
     * @param mobile
     * @param passwordOld
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updatePassword")
    public BaseResponseVo updatePassword(@RequestParam(value = "mobile")String mobile ,@RequestParam(value = "passwordOld")String passwordOld
                                        ,@RequestParam(value = "password")String password) {

        User user = userService.queryByMobile(mobile);
        if(user==null){
            return new BaseResponseVo(StatusCode.USER_NOT_EXITS.getCode(), StatusCode.USER_NOT_EXITS.getMessage());
        }
        if(!user.getPassword().equalsIgnoreCase(MD5Util.MD5(passwordOld))){
            return new BaseResponseVo(StatusCode.PASSWORD_ERROR.getCode(), StatusCode.PASSWORD_ERROR.getMessage());
        }
        user.setPassword(MD5Util.MD5(password));
        int result = userService.update(user);
        if(result==0){
            return new BaseResponseVo(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMessage());
        }
        return new BaseResponseVo(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
    }

    /**
     * 重置密码
     * @param mobile
     * @param password
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/resetPassword")
    public BaseResponseVo resetPassword(@RequestParam(value = "mobile")String mobile,@RequestParam(value = "password")String password
                                        ,@RequestParam(value = "code")String code) {

        User user = userService.queryByMobile(mobile);
        if(user==null){
            return new BaseResponseVo(StatusCode.USER_NOT_EXITS.getCode(), StatusCode.USER_NOT_EXITS.getMessage());
        }

        //验证短信code
        String code1 =(String)redisClient.get("resetPassword", user.getMobile(), String.class);
        if(!code.equalsIgnoreCase(code1)){
            return new BaseResponseVo(StatusCode.CODE_ERROR.getCode(), StatusCode.CODE_ERROR.getMessage());
        }
        user.setPassword(MD5Util.MD5(password));
        int result = userService.update(user);
        if(result==0){
            return new BaseResponseVo(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMessage());
        }
        return new BaseResponseVo(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
    }


}

